var Api = require('../../utils/api.js');
var util = require('../../utils/util.js');
var Auth = require('../../utils/auth.js');

var wxApi = require('../../utils/wxApi.js')
var wxRequest = require('../../utils/wxRequest.js');
var app = getApp();
var webSiteName = config.getWebsiteName;
var domain = config.getDomain
import config from '../../utils/config.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        windowHeight: '100%',
        date: '请选择开始时间',
        date2: '请选择结束时间',
        list: [],
        keyword: '',
        status: 2,
        page: 1,
        page_total: 0,
        type: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var self = this;
        self.setData({
            type: options.type
        })
        wx.getSystemInfo({
            success: (result) => {
                self.setData({
                    windowHeight: result.windowHeight,
                    windowWidth: result.windowWidth
                })
                if (result.inFinChat) {
                    self.setData({
                        inFinChat: result.inFinChat
                    })
                } else {
                    Auth.setUserInfoData(self);
                    Auth.checkLogin(self);
                    self.setData({
                        copyright: getApp().globalData.copyright
                    })
                    Auth.checkSession(self, 'isLoginNow');
                }
            },
        })

        
        wx.request({
            url: 'https://a.ygtape.com.cn/index.php/qualityassurance/index/list',
            method: 'post',
            data: {
                key_words: options.keyword,
                type: self.data.type,
                status: self.data.status,
                mobile: self.data.userInfo.user_mobile,
                token: self.data.userInfo.token,
                page: self.data.page
            },
            success: function (res) {
                console.log(res);
                self.setData({
                    page_total: res.data.total,
                    list: res.data.rows
                    // list:res.data.list
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    bindDateChange: function (e) {
        // console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            date: e.detail.value
        })
    },
    bindDateChange2: function (e) {
        // console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            date2: e.detail.value
        })
    },
    goToDetail: function (e) {
        wx.navigateTo({
            url: '/pages/warrantyDetail/detail?id=' + e.target.dataset.id,
        })
    },
    getinputVlue: function (e) {
        this.setData({
            keyword: e.detail.value
        })
    },
    selectStatus: function (e) {
        var that = this;
        var status = e.detail.index + 1
        that.setData({
            status: status,
            page: 1,
            list: [],
            page_total: 0
        })
        var list = that.searchList(e, 1);
    },

    bindscrolltolower: function (e) {
        var that = this;

        if (that.data.page_total >= that.data.page) {
            var now_page = that.data.page + 1;
            that.setData({
                page: now_page
            })
            
            this.appendList();
        }

    },
    //搜索不追加 直接替换 下拉追加
    searchList: function (e, type) {
        var self = this;

        if(!self.data.userInfo.user_mobile){
            wx.showToast({
                title: '太久未操作，请重新登录授权!',
                icon: 'none',
                duration: 2000
            });
            return false;
        }
        console.log(self.data.page);

        wx.request({
            url: 'https://a.ygtape.com.cn/index.php/qualityassurance/index/list',
            method: 'post',
            data: {
                search: self.data.keyword,
                type: self.data.type,
                date_start: self.data.date,
                date_end: self.data.date2,
                status: self.data.status,
                page: 1,
                mobile: self.data.userInfo.user_mobile,
                token: self.data.userInfo.token,
            },
            success: function (res) {
                var list_data = [];
                self.setData({
                    page_total: res.data.total
                })
                for (let i in res.data.rows) {
                    list_data.push(res.data.rows[i])
                }
                self.setData({
                    list: list_data,
                    page: 1
                })
            }
        })

    },
    //搜索不追加 直接替换 下拉追加
    appendList: function (e, type) {
        var self = this;
        wx.request({
            url: 'https://a.ygtape.com.cn/index.php/qualityassurance/index/list',
            method: 'post',
            data: {
                search: self.data.keyword,
                type: self.data.type,
                date_start: self.data.date,
                date_end: self.data.date2,
                status: self.data.status,
                page: self.data.page,
                mobile: self.data.userInfo.user_mobile,
                token: self.data.userInfo.token,
            },
            success: function (res) {
                var list = self.data.list;

                if(res.data.rows <= 0){
                   
                    wx.showToast({
                        title: '没有更多数据了!',
                        icon: 'none',
                        duration: 2000
                    });

                    return false;
                }

                for(let i in res.data.rows){
                    list.push( res.data.rows[i])
                }
                self.setData({
                    list:list
                })

            }
        })

    }
})